package require1;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;

import java.util.Map;

public class WebLogBolt implements IRichBolt {
    private OutputCollector collector = null;
    private String valueString = null;
    private int line_num = 0;


    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        this.collector = outputCollector;
    }



    @Override
    public void execute(Tuple input) {

        try {
            // 1 获取传递过来的数据
            valueString = input.getStringByField("log");

            // 2 如果输入的数据不为空，行数++
            if (valueString != null) {
                line_num++;
                System.err.println(Thread.currentThread().getId() + "lines  :" + line_num + "   session_id:" + valueString.split("\t")[1]);
            }

            // 3 应答Spout接收成功
            collector.ack(input);

            Thread.sleep(2000);
        } catch (Exception e) {
            // 4 应答Spout接收失败
            collector.fail(input);

            e.printStackTrace();
        }

    }

    @Override
    public void cleanup() {
        //清除资源
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        //声明
        outputFieldsDeclarer.declare(new Fields(""));
    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        //获取配置信息
        return null;
    }
}
